/**第234题*/
//请判断一个链表是否为回文链表。 
//
// 示例 1: 
//
// 输入: 1->2
//输出: false 
//
// 示例 2: 
//
// 输入: 1->2->2->1
//输出: true
// 
//
// 进阶： 
//你能否用 O(n) 时间复杂度和 O(1) 空间复杂度解决此题？ 
// Related Topics 链表 双指针 
// 👍 815 👎 0

package tree.leetcode.editor.cn;

import com.dq.node.List;
import com.dq.node.ListNode;


public class PalindromeLinkedList {
    public static void main(String[] args) {
        Solution solution = new PalindromeLinkedList().new Solution();
        ListNode head = List.newList(new int[]{1,0,1});
        List.showList(head);
        System.out.println(solution.isPalindrome(head));
    }
        //leetcode submit region begin(Prohibit modification and deletion)
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
    class Solution {
        private ListNode left = null;
        public boolean isPalindrome(ListNode head) {
            if(head==null) return true;
            left = head;
            return travel(head);
        }
        public boolean travel(ListNode right){

            boolean res = true;
            if(right.next!=null){
                res = travel(right.next);
            }
            res = res && right.val==left.val;
            left = left.next;
            return res;
        }
        public ListNode reverseList(ListNode head){

            if (head == null || head.next==null) return head;
            ListNode newHead = reverseList(head.next);
            head.next.next = head;
            head.next = null;
            return newHead;
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}